//
//  UIControl+ActionBlocks.m
//  JQKit
//
//  Created by JQZ on 2017/12/27.
//  Copyright © 2017年 JQZ. All rights reserved.
//

#import "UIControl+ActionBlocks.h"
#import <objc/runtime.h>

#define JQ_UICONTROL_EVENT( methodName, eventName ) \
- (void)methodName:(void (^)(void))eventBlock \
{ \
    objc_setAssociatedObject(self, @selector(methodName:), eventBlock, OBJC_ASSOCIATION_COPY_NONATOMIC); \
    [self addTarget:self action:@selector(methodName##Action:) forControlEvents:UIControlEvent##eventName]; \
}\
- (void)methodName##Action:(id)sender \
{ \
    void (^block)(void) = objc_getAssociatedObject(self, @selector(methodName:)); \
    if (block) { \
        block(); \
    } \
}

@implementation UIControl (ActionBlocks)

JQ_UICONTROL_EVENT(jq_touchDown, TouchDown)
JQ_UICONTROL_EVENT(jq_touchDownRepeat, TouchDownRepeat)
JQ_UICONTROL_EVENT(jq_touchDragInside, TouchDragInside)
JQ_UICONTROL_EVENT(jq_touchDragOutside, TouchDragOutside)
JQ_UICONTROL_EVENT(jq_touchDragEnter, TouchDragEnter)
JQ_UICONTROL_EVENT(jq_touchDragExit, TouchDragExit)
JQ_UICONTROL_EVENT(jq_touchUpInside, TouchUpInside)
JQ_UICONTROL_EVENT(jq_touchUpOutside, TouchUpOutside)
JQ_UICONTROL_EVENT(jq_touchCancel, TouchCancel)
JQ_UICONTROL_EVENT(jq_valueChanged, ValueChanged)
JQ_UICONTROL_EVENT(jq_editingDidBegin, EditingDidBegin)
JQ_UICONTROL_EVENT(jq_editingChanged, EditingChanged)
JQ_UICONTROL_EVENT(jq_editingDidEnd, EditingDidEnd)
JQ_UICONTROL_EVENT(jq_editingDidEndOnExit, EditingDidEndOnExit)


@end
